
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing;
using System.Threading;

namespace WindowsFormsApplication1
{


	public class AnalisadorDTW : Analisador
	{

		private int[] anterior = new int[0];
		private Bdac bdac = new Bdac();
		private QrsFilt filt = new QrsFilt();
		private DTW dtw = null;
		private List<int> listaDiferençcas = new List<int>();
		private int pont = 4000;
		
		public AnalisadorDTW (int[] amostras)
		{
			bdac.ResetBDAC();
			
			int sampleCount = 0;
			int DetectionTime = 0;
			int aux = 0;
			
			while(dtw == null)
            {
                ++sampleCount;
				int fDatum = filt.filter(amostras[sampleCount]);
				int delay = bdac.BeatDetect(fDatum,sampleCount);
				  if (delay!= 0)
                {
					 DetectionTime =sampleCount - delay;
					if(aux!=0){
						 dtw = new DTW(ArrayUtil.getSubArray(amostras,aux,DetectionTime));					
					}		
					aux = DetectionTime;
				}
			}
			
			anterior = ArrayUtil.getSubArray(amostras,DetectionTime, amostras.Length);
		}
		
		public List<int> getListaDiferencas()
		{
			return listaDiferençcas;
		}
		
		public void analisarTrecho(int[] amostras){
			amostras = atualizaAmostra(amostras);
			int sampleCount = 0;
			
			int aux = 0;
		 	int DetectionTime = 0;
			for (int i = anterior.Length; i < amostras.Length-1; i++)
            {
				
               	int fDatum = filt.filter(amostras[i]);
				int delay = bdac.BeatDetect(fDatum,i);
   				if (delay!= 0)
                {
					 	DetectionTime = i - delay;
				//		Console.Write((pont+DetectionTime).ToString()+" - ");
						listaDiferençcas.Add(dtw.calcularDistancia(ArrayUtil.getSubArray(amostras,aux,DetectionTime)));
						aux = DetectionTime;					
				}
			}
			pont+=4000;
			anterior = ArrayUtil.getSubArray(amostras,DetectionTime,amostras.Length-1);
			amostras = null;
		}
		
		private int[] atualizaAmostra(int[] amostrasOriginal)
		{
			int[] meuArray = new int[anterior.Length+amostrasOriginal.Length];
			
			anterior.CopyTo(meuArray,0);
			amostrasOriginal.CopyTo(meuArray,anterior.Length);
			
			return meuArray;
			
		}
	}
}
			    			
          
